/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modulos.consulta.oftalmologia.vista;

import basedatos.OperacionesBD;
import basedatos.RecetaOftalmologiaBD;
import java.awt.event.KeyEvent;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import modulos.consulta.oftalmologia.modelo.RecetaOftalmologia;
import modulos.paciente.modelo.Paciente;
import vista.frmPrincipal;
import vista.modelo.Mensaje;
import vista.modelo.OperacionesVarias;

/**
 *
 * @author medikux
 */
public class iFrmListaRecetaOftalmologia extends javax.swing.JInternalFrame {

    /**
     * Creates new form iFrmListaReceta
     */
    public iFrmListaRecetaOftalmologia() {
        initComponents();
        OperacionesVarias.centrar(this);
    }
    
    public iFrmListaRecetaOftalmologia(Paciente p) {
        initComponents();
        OperacionesVarias.centrar(this);
        paciente = p;
        listarRecetas();
        modeloTabla();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnCerrar = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblRecetas = new JTable() {
            public boolean isCellEditable(int rowIndex, int vColIndex) {
                return false;
            }
        };
        jLabel2 = new javax.swing.JLabel();
        btnNuevo = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();

        setTitle("LISTADO DE RECETA DE LENTES");
        getContentPane().setLayout(null);

        btnCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/agt_home.png"))); // NOI18N
        btnCerrar.setText("Cerrar");
        btnCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCerrarActionPerformed(evt);
            }
        });
        getContentPane().add(btnCerrar);
        btnCerrar.setBounds(234, 322, 130, 35);

        jPanel2.setBackground(java.awt.Color.gray);

        tblRecetas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tblRecetas.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tblRecetasKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(tblRecetas);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 273, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        getContentPane().add(jPanel2);
        jPanel2.setBounds(35, 43, 452, 273);

        jLabel2.setFont(new java.awt.Font("DejaVu Sans", 3, 16)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(21, 64, 205));
        jLabel2.setText("Recetas de Lentes");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(210, 10, 167, 19);

        btnNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/add.png"))); // NOI18N
        btnNuevo.setText("Nuevo");
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });
        getContentPane().add(btnNuevo);
        btnNuevo.setBounds(493, 43, 100, 27);

        btnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/icono_editar.png"))); // NOI18N
        btnEditar.setText("Editar");
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });
        getContentPane().add(btnEditar);
        btnEditar.setBounds(493, 80, 100, 27);

        btnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/delete.png"))); // NOI18N
        btnEliminar.setText("Eliminar");
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });
        getContentPane().add(btnEliminar);
        btnEliminar.setBounds(493, 117, 100, 27);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-620)/2, (screenSize.height-399)/2, 620, 399);
    }// </editor-fold>//GEN-END:initComponents

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        if (tblRecetas.getSelectedRow() != -1) {
            eliminarReceta();
        } else {
            JOptionPane.showMessageDialog(null, "No ha seleccionado ninguna receta");
        }
    }//GEN-LAST:event_btnEliminarActionPerformed
    
    private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCerrarActionPerformed
        dispose();        // TODO add your handling code here:
    }//GEN-LAST:event_btnCerrarActionPerformed
    
    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
        iFrmRecetaLentesOftalmologia frl = new iFrmRecetaLentesOftalmologia(paciente);
        frmPrincipal.dskpPrincipal.add(frl);
        frl.toFront();
        frl.setVisible(true);
    }//GEN-LAST:event_btnNuevoActionPerformed
    
    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
        if (tblRecetas.getSelectedRow() != -1) {
            receta = recetas.get(tblRecetas.getSelectedRow());
            iFrmRecetaLentesOftalmologia frl = new iFrmRecetaLentesOftalmologia(receta, paciente, 1);
            frmPrincipal.dskpPrincipal.add(frl);
            frl.toFront();
            frl.setVisible(true);
        } else {
            Mensaje.filaNoSeleccionada("Paciente");
        }
    }//GEN-LAST:event_btnEditarActionPerformed
    
    private void tblRecetasKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tblRecetasKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_DELETE) {            
            if (tblRecetas.getSelectedRow() != -1) {
                eliminarReceta();
            } else {
                JOptionPane.showMessageDialog(null, "No ha seleccionado ninguna receta");
            }
        }
    }//GEN-LAST:event_tblRecetasKeyPressed
    private void eliminarReceta() {
        int n = JOptionPane.showConfirmDialog(null, "¿Esta seguro de querer eliminar esta receta?", "Eliminar", JOptionPane.YES_NO_OPTION);
        if (n == JOptionPane.YES_OPTION) {
            receta = recetas.get(tblRecetas.getSelectedRow());
            if (OperacionesBD.eliminar(receta)) {
                Mensaje.datosEliminados();
                listarRecetas();
                modeloTabla();
            } else {
                Mensaje.datosNoEliminados();
            }
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCerrar;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    public static javax.swing.JTable tblRecetas;
    // End of variables declaration//GEN-END:variables
    public static ArrayList<RecetaOftalmologia> recetas;
    public static Paciente paciente;
    public static RecetaOftalmologia receta;
    
    public static void listarRecetas() {
        try {
            recetas = (ArrayList<RecetaOftalmologia>) RecetaOftalmologiaBD.listaRecetasPorPaciente(paciente);
        } catch (Exception e) {
            Logger.getLogger(iFrmListaRecetaOftalmologia.class.getName()).log(Level.SEVERE, null, e);
        }
    }
    
    public static void modeloTabla() {
        
            DefaultTableModel dtm = new DefaultTableModel();
            dtm.addColumn("Nro");
            dtm.addColumn("Fecha");
            int cont = 0;
            for (RecetaOftalmologia r : recetas) {
                Object[] obj = new Object[2];
                cont++;
                obj[0] = cont;
                DateFormat d = DateFormat.getDateInstance(DateFormat.SHORT);
                obj[1] = d.format(r.getFechaReceta());
                dtm.addRow(obj);
            }
            tblRecetas.setModel(dtm);
        
    }
}
